import tkinter as tk
import random, json, pyperclip, os, pyautogui, win32gui, win32con
from auto_craft.parts.role_controller import RoleController
from functools import partial
from libs import get_image_pos, focus_wow_handle
from time import sleep, time

class AutoCraftController:
  def __init__(self, tk_root) -> None:
    self.tk_root = tk_root
    self.build_ui()
    self.cur_idx = -1
    self.role_list = self.build_role_list()
    
  def build_role_list(self):
    role_list_bl1 = ["泽塔丶", "小羊苏西丶", "小象丶艾德蒙", "小象丶艾米丽", "斑马丶莎莎", "小兔丶罗比"]
    role_list_lm1 = ["小狗丹尼丶", "小熊贝琳达丶", "袋鼠凯莉丶", "小兔理查德丶", "小毛驴戴芬丶", "小黑萨", "艾斯比丶", "工神黄月英", "强壮的如花", "徐元植",]
    role_list_lm2 = ["火星野人", "小马佩德罗丶", "很丶强", "壮士请灭灯", "联盟酋長", "机械狗", "王立宏", "奥丁尼尔", "希瓦的守護"]
    role_list_lm3 = ["机械火鸡", "狐狸弗雷德丶", "小熊猫丶佩吉", "小狮子丶里奥", "小老虎丶泰格", "坤丶乔治", "坎丶龙人", "坤丶佩奇", "斑马苏怡丶"]
    role_list_lm4 = ["袋鼠丶乔伊", "小松鼠丶西蒙", "机械法丝", "壮汉诸葛恪", "斑马丶祖祖", "小毛驴丶迪安", "上将丷潘凤", "联盟大酋長",]
    role_list_lm5 = ["斯巴达土炮", "風很大", "黄巾雷使", "姜铁锤", "董贼别跑", "闭丶月", "头狼骑士", "吕丷布", "如花大哥"]
    role_list_bl2 = ["狼人酋長", "吉恩国王", "柚乄子"]
    role_list_bl3 = ["柚乄子", "兽丶兽", "狼王吉恩"]
    return role_list_bl1 + role_list_lm1 + role_list_lm2 + role_list_lm3 + role_list_lm4 + role_list_lm5 + role_list_bl2
  
  def build_ui(self):
    tk.Button(self.tk_root, text="收菜", command=self.start_craft_task).place(x=10, y=170, width=80, height=30)
    tk.Button(self.tk_root, text="拆卡牌", command=self.start_salvage_task).place(x=100, y=170, width=80, height=30)
    tk.Button(self.tk_root, text="键位", command=self.start_set_keyboard_task).place(x=190, y=170, width=80, height=30)
    tk.Button(self.tk_root, text="订单", command=self.start_order_task).place(x=280, y=170, width=80, height=30)
    tk.Button(self.tk_root, text="停止", command=self.stop_task).place(x=370, y=170, width=80, height=30)

  def start_set_keyboard_task(self):
    role_list = self.role_list
    self.cur_idx += 1
    if self.cur_idx >= len(role_list):
      self.cur_idx = -1
      print("All role done!!")
      return
    role = RoleController(role_name=role_list[self.cur_idx], action="set_keyboard")
    role.start(self.start_set_keyboard_task)
  
  def start_order_task(self):
    role_list = self.role_list
    self.cur_idx += 1
    if self.cur_idx >= len(role_list):
      self.cur_idx = -1
      print("All role done!!")
      return
    role = RoleController(role_name=role_list[self.cur_idx], action="order")
    role.start(self.start_order_task)

  def start_craft_task(self):
    role_list = self.role_list
    self.cur_idx += 1
    if self.cur_idx >= len(role_list):
      self.cur_idx = -1
      print("All role done!!")
      return
    role = RoleController(role_name=role_list[self.cur_idx], action="craft")
    role.start(self.start_craft_task)
  
  def start_salvage_task(self):
    role_list = self.role_list
    self.cur_idx += 1
    if self.cur_idx >= len(role_list):
      self.cur_idx = -1
      print("All role done!!")
      return
    role = RoleController(role_name=role_list[self.cur_idx], action="salvage")
    role.start(self.start_salvage_task)

  def stop_task(self):
    pos = self.get_qs_pos()
    print(pos)

  def get_qs_pos(self):
    focus_wow_handle()
    sleep(1)
    count = 1
    pos = None
    while (pos is None) and (count < 5):
      sleep(1)
      pos = get_image_pos("images_2/word_dz_order_success.png", confidence = 0.8)
      count += 1
    return pos